import { exec } from 'child_process';
import { BrowserWindow } from 'electron';
import { promisify } from 'util';
const execAsync = promisify(exec);

export const execBash = async (cmd) => {
  try {
    console.log({ cmd });
    const { stdout, stderr } = await execAsync(cmd);
    if (stderr) console.error({ stderr });
    return stdout;
  } catch (error) {
    console.error(error);
    return null;
  }
};

export const execCopyPaste = async (content) => {
  const mainWin = BrowserWindow.getFocusedWindow();
  mainWin.hide();
  setTimeout(() => {
    console.log(content);
    exec(`osascript -e '
   set the clipboard to "${content.replace(/"/g, '\\"')}"
   tell app "System Events" to keystroke "v" using command down
 '`);
  }, 100);
};
